﻿// Decompiled with JetBrains decompiler
// Type: System.Web.Razor.Generator.RazorCodeGenerator
// Assembly: System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// MVID: 682513D6-F37E-48AC-9909-F3EE103CFF14
// Assembly location: D:\Project\Idea\无人机外网\uav\客户端工程\排查工具\bin\Release\System.Web.Razor.dll

using Microsoft.Internal.Web.Utils;
using System.CodeDom;
using System.Linq;
using System.Web.Razor.Parser;
using System.Web.Razor.Parser.SyntaxTree;

namespace System.Web.Razor.Generator
{
  /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Represents a Razor code generator.</summary>
  public abstract class RazorCodeGenerator : ParserVisitor
  {
    private CodeGeneratorContext _context;

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Initializes a new instance of the <see cref="T:System.Web.Razor.Generator.RazorCodeGenerator" /> class.</summary>
    /// <param name="className">The class name.</param>
    /// <param name="rootNamespaceName">The root namespace name.</param>
    /// <param name="sourceFileName">The source file name.</param>
    /// <param name="host">The host.</param>
    protected RazorCodeGenerator(
      string className,
      string rootNamespaceName,
      string sourceFileName,
      RazorEngineHost host)
    {
      if (string.IsNullOrEmpty(className))
        throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, nameof (className));
      if (rootNamespaceName == null)
        throw new ArgumentNullException(nameof (rootNamespaceName));
      if (host == null)
        throw new ArgumentNullException(nameof (host));
      this.ClassName = className;
      this.RootNamespaceName = rootNamespaceName;
      this.SourceFileName = sourceFileName;
      this.GenerateLinePragmas = !string.IsNullOrEmpty(this.SourceFileName);
      this.Host = host;
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets or sets the class name for this code.</summary>
    /// <returns>The class name for this code.</returns>
    public string ClassName { get; private set; }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets or sets the name of the root namespace.</summary>
    /// <returns>The name of the root namespace.</returns>
    public string RootNamespaceName { get; private set; }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets or sets the name of the source file.</summary>
    /// <returns>The name of the source file.</returns>
    public string SourceFileName { get; private set; }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets or sets the razor engine host.</summary>
    /// <returns>The razor engine host.</returns>
    public RazorEngineHost Host { get; private set; }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets or sets a value indicating whether the generator should generate line pragmas in the Razor code.</summary>
    /// <returns>true if the generator should generate line pragmas in the Razor code; otherwise, false.</returns>
    public bool GenerateLinePragmas { get; set; }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets or sets a value indicating whether the code generator is in design-time mode.</summary>
    /// <returns>true if the code generator is in design-time mode; otherwise, false.</returns>
    public bool DesignTimeMode { get; set; }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets the context of this code generator.</summary>
    /// <returns>The context of this code generator.</returns>
    public CodeGeneratorContext Context
    {
      get
      {
        this.EnsureContextInitialized();
        return this._context;
      }
    }

    internal virtual Func<CodeWriter> CodeWriterFactory => (Func<CodeWriter>) null;

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Visits the start block.</summary>
    /// <param name="block">The block to visit.</param>
    public override void VisitStartBlock(System.Web.Razor.Parser.SyntaxTree.Block block) => block.CodeGenerator.GenerateStartBlockCode(block, this.Context);

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Visits the end block.</summary>
    /// <param name="block">The block to visit.</param>
    public override void VisitEndBlock(System.Web.Razor.Parser.SyntaxTree.Block block) => block.CodeGenerator.GenerateEndBlockCode(block, this.Context);

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Visits the span.</summary>
    /// <param name="span">The span to visit.</param>
    public override void VisitSpan(Span span) => span.CodeGenerator.GenerateCode(span, this.Context);

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Raises the Complete event.</summary>
    public override void OnComplete() => this.Context.FlushBufferedStatement();

    private void EnsureContextInitialized()
    {
      if (this._context != null)
        return;
      this._context = CodeGeneratorContext.Create(this.Host, this.CodeWriterFactory, this.ClassName, this.RootNamespaceName, this.SourceFileName, this.GenerateLinePragmas);
      this.Initialize(this._context);
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Initializes the current <see cref="T:System.Web.Razor.Generator.RazorCodeGenerator" /> instance.</summary>
    /// <param name="context">The context.</param>
    protected virtual void Initialize(CodeGeneratorContext context)
    {
      context.Namespace.Imports.AddRange(this.Host.NamespaceImports.Select<string, CodeNamespaceImport>((Func<string, CodeNamespaceImport>) (s => new CodeNamespaceImport(s))).ToArray<CodeNamespaceImport>());
      if (!string.IsNullOrEmpty(this.Host.DefaultBaseClass))
        context.GeneratedClass.BaseTypes.Add(new CodeTypeReference(this.Host.DefaultBaseClass));
      CodeTypeMemberCollection members = context.GeneratedClass.Members;
      CodeConstructor codeConstructor = new CodeConstructor();
      codeConstructor.Attributes = MemberAttributes.Public;
      members.Add((CodeTypeMember) codeConstructor);
    }
  }
}
